#include "app.h"

//
// Examples:
//      char* user_cmd = "r 0x60004000 0x70 25:0";
//      char* user_cmd = "w 0x60004070 0x34 8:0 0x5";
//
// return 0 or -1
//
RETCODE user_cmd_parse(char* user_cmd, struct user_cmd_t* cmd)
{
    char flag        = 0;
    U32  addr_base   = 0;
    U32  addr_offset = 0;
    U32  bit_start   = 0;
    U32  bit_end     = 0;
    U32  bit_value   = 0;
    U32  ret;

    GOTO_FAILED((NULL == user_cmd) || (NULL == cmd));

    if (user_cmd[0] == 'r')
    {
        ret = sscanf(user_cmd, "%c %x %x %d:%d", &flag, &addr_base, &addr_offset, &bit_end, &bit_start);
        GOTO_FAILED(5 != ret);
    }
    else if (user_cmd[0] == 'w')
    {
        ret = sscanf(user_cmd, "%c %x %x %d:%d %x", &flag, &addr_base, &addr_offset, &bit_end, &bit_start, &bit_value);
        GOTO_FAILED(6 != ret);
    }
    else
    {
        GOTO_FAILED(1);
    }

    cmd->cmd  = flag;
    cmd->reg_addr = addr_base + addr_offset;
    cmd->bit_start = bit_start;
    cmd->bit_end   = bit_end;
    cmd->bit_value = bit_value;

    return RET_OK;

FAILED:
    return RET_FAILED;
}
